<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义事件</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <h3>{{messages}}</h3>
    <child-component v-on:notify="childMessage"></child-component>
</div>

<template id="child-component-tmpl">
    <div>
        <input v-model="message">
        <button @click="notify">发送</button>
    </div>
</template>

<script>
    Vue.component('child-component', {
        template: '#child-component-tmpl',
        data: function () {
            return {
                message: ''
            };
        },
        methods: {
            notify: function () {
                if (this.message.trim()) {
                    this.$emit('notify', this.message);
                    this.message = '';
                }
            }
        }
    });
    new Vue({
        el: '#app',
        data: {
            messages: []
        },
        methods: {
            childMessage: function (message) {
                this.messages.push(message);
            }
        }
    });
</script>
</body>
</html>